<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="../plugins/select2/select2.css" />
    <link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
    <script src="../plugins/select2/select2.min.js" type="text/javascript"></script>

</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteId()"><i class="fa fa-trash-o"></i> 删除</button>
                                       
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                  分类模板名称：<input type="text" id="name2">
									<input type="button" onclick="getData()" class="btn btn-default" value="查询"/>
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">

			                  </table>
			                  <!--数据列表/-->
							<a href="javascript:toPage(3)">首页</a>
							<a href="javascript:toPage(1)">上一页</a>
							<a href="javascript:toPage(2)">下一页</a>
							<a href="javascript:toPage(4)">尾页</a>
							当前第<input type="text" id="pageNumber" size="1">页
							每页<input type="text" id="pageSize" size="1">条
							共<input type="text" id="totalPage" readonly="readonly" size="1">页
							共<input type="text" id="totalCount" readonly="readonly" size="1">条
                        </div>
                        <!-- 数据表格 /-->
                        
                     </div>
                    <!-- /.box-body -->
                    
	           
					    
                                
<!-- 增加窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品类型模板编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>商品类型</td>
		      		<td><input  class="form-control" id="name" placeholder="商品类型">  </td>
		      	</tr>			   
		      	<tr>
		      		<td>关联品牌</td>
		      		<td>
		      			<select id="brand" multiple style="width: 100%"></select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td>
						<select id="spec" multiple style="width: 100%"></select>
		      		</td>
		      	</tr>	
		      	
		      	<tr>
		      		<td>扩展属性</td>
		      		<td>
		      			<div class="btn-group">
                             <button type="button" onclick="addTable(1)" class="btn btn-default" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>
                                                                    
                        </div>
						<table class="table table-bordered table-striped" id="specOption"  width="800px">
							<thead>
								<tr>
								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>									
									<td>操作</td>
								</tr>
							</thead>
							<tbody id="addForm">

							</tbody>
						</table>	
           
		      		</td>
		      	</tr>	
		      	      
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="save()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>



  <!-- 修改窗口 -->
<div class="modal fade" id="editMod" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLa">商品类型模板编辑</h3>
		</div>
		<div class="modal-body">

			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
					<td>
						<input type="hidden"  id="id">
					</td>
		      		<td>商品类型</td>
		      		<td><input  class="form-control" id="name1" placeholder="商品类型">  </td>
		      	</tr>
		      	<tr>
		      		<td>关联品牌</td>
		      		<td>
		      			<select id="brand1" multiple style="width: 100%"></select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td>
						<select id="spec1" multiple style="width: 100%"></select>
		      		</td>
		      	</tr>

		      	<tr>
		      		<td>扩展属性</td>
		      		<td>
		      			<div class="btn-group">
                             <button type="button" onclick="addTable(2)" class="btn btn-default" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>

                        </div>
						<table class="table table-bordered table-striped" id="specOption1"  width="800px">
							<thead>
								<tr>
								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>
									<td>操作</td>
								</tr>
							</thead>
							<tbody id="updateForm">

							</tbody>
						</table>

		      		</td>
		      	</tr>

			 </table>

		</div>
		<div class="modal-footer">
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateTo()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
    
</body>

</html>
	<script>
		$(function () {
			getData()
			getBrandsList()
			getSpec()
		})

		function addTable(flag){
			var table= "<tr>";
			table += "<td>";
			table += " <input type=\"checkbox\" class=\"icheckbox_square-blue\" >";
			table += "</td>";
			table += "<td>";
			table += "<input class=\"form-control\" placeholder=\"属性名称\" >";
			table += "</td>";
			table += "<td>";
			table += "<button type=\"button\" class=\"btn btn-default\" title=\"删除\" onclick='deleteTable(this)'><i class=\"fa fa-trash-o\"></i> 删除</button>";
			table += "</td>";
			table += "</tr>";
			if (flag ==1){
				$("#addForm").append(table);
			}
			if (flag ==2){
				$("#updateForm").append(table)
			}
		}
		//删除
		function deleteTable(obj){
			$(obj).parent().parent().remove();
		}


		//关联品牌
		function getBrandsList() {
			$.ajax({
				url: "/tbBrandController/getShow",
				type: "post",
				dataType: "json",
				async: true,
				success: function (result) {
					if (result.code == 10000) {
						var option = "";
						var list = result.data
						for (let i = 0; i < list.length; i++) {
							option += '<option value="' + list[i].id + '">';

							option += list[i].name;
							option += '</option>';
						}
						$("#brand").html(option)
						$("#brand").select2()
						$("#brand1").html(option)
						$("#brand1").select2()

					}

				},
				error: function () {
					alert("系统错误")
				}
			})
		}
			//关联规格
		function getSpec() {
			$.ajax({
				url: "/tbSpecification/getShowTo",
				type: "post",
				dataType: "json",
				async: true,
				success: function (result) {
					if (result.code ==10000){
						var option = "";
						var list = result.data
						for (let i = 0; i < list.length; i++) {
							option +='<option value="'+list[i].id+'">';
							option +=list[i].specName;
							option +='</option>';
						}
						$("#spec").html(option)
						$("#spec").select2()
						$("#spec1").html(option)
						$("#spec1").select2()
					}
				},
				error:function () {
					alert("系统错误")
				}
			})
		}


		//保存方法
		function save() {
			//关联品牌
			var brands= $("#brand").select2('data')
			var arrBrands = []
			$.each(brands,function (i,e) {

			var json =	{"id":e.id,"text":e.text}
				arrBrands.push(json)
			})
			var arrJson = JSON.stringify(arrBrands)

			//关联型号
			var spec= $("#spec").select2('data')
			var arr = []
			$.each(spec,function (i,e) {
			var json =	{"id":e.id,"text":e.text}
				arr.push(json)
			})
			var arrSpecJson = JSON.stringify(arr)

			//商品类型
			var  name = $("#name").val()

			//获取拓展的值
			var optionList = [];
			var trs = $("#specOption").find("tr");
			for (let i = 1; i <trs.length; i++) {
				var  tds = $(trs[i]).find("td");
				var orders = $(tds[1]).find("input").val();
				optionList.push({text:orders});
			}
			var optionListJson= JSON.stringify(optionList)
			$.ajax({
				url: "/tbTypeTemplateController/save",
				type: "post",
				dataType: "json",
				data:{
						name : name,
					specIds : arrSpecJson,
					brandIds : arrJson,
					customAttributeItems : optionListJson
				},
				async: true,
				success: function (result) {
					if(result.code==10000){
						alert(result.message);
						window.location.reload();
					}
				},
				error:function () {
					alert("系统错误")
				}
			})
		}


		function getData() {
			$.ajax({
				url: "/tbTypeTemplateController/getInfo",
				type: "post",
				data:{pageNumber: $("#pageNumber").val() ,name : $("#name2").val()},
				dataType: "json",
				async: true,
				success: function (data) {
					var show = data.data
					let result = show.list;
					for (var i = 0; i < result.length; i++) {
						var  specJson =JSON.parse(result[i].specIds)//将json格式的字符串转换成json对象
						var  brandJson =JSON.parse(result[i].brandIds)//将json格式的字符串转换成json对象
						var  ItemsJson =JSON.parse(result[i].customAttributeItems)//将json格式的字符串转换成json对象

						var strSpec = "";
						var strBrand="";
						var strItems="";

						for (let j = 0; j < specJson.length; j++) {
							strSpec += specJson[j].text
						}
						for (let j = 0; j < brandJson.length; j++) {
							strBrand += brandJson[j].text
						}
						for (let j = 0; j < ItemsJson.length; j++) {
							strItems += ItemsJson[j].text
						}
						strSpec.substring(1)
						strBrand.substring(1)
						strItems.substring(1)
					}
					var table = " <thead>";
					table += "<tr>";
					table += " <th class=\"\" style=\"padding-right:0px\">";
					table += " <input id=\"selall\" type=\"checkbox\" class=\"icheckbox_square-blue\">";
					table += "</th>";
					table += "<th  class=\"sorting_asc\">模板ID</th>";
					table += "<th class=\"sorting\">分类模板名称</th>";
					table += "<th class=\"sorting\">关联品牌</th>";
					table += "<th class=\"sorting\">关联规格</th>";
					table += "<th class=\"sorting\">扩展属性</th>";
					table += "<th class=\"text-center\">操作</th>";
					table += "</tr>";
					table += " </thead>";

					for (var i = 0; i < result.length; i++) {
						table += "<tbody>";
						table += "<tr>";
						table += "<td>"
						table +="<input  type=\"checkbox\" value='"+result[i].id +"' name='check'> "
						table +="</td>"
						table += "<td> "+ result[i].id + "</td>";
						table += "<td>" + result[i].name + "</td>";
						table += "<td>" + strBrand + "</td>";
						table += "<td>" + strSpec + "</td>";
						table += "<td>" + strItems + "</td>";
						table += "<td class='text-center'> ";
						table += " <input type=\"button\" class=\"btn bg-olive btn-xs\" data-toggle=\"modal\"  data-target=\"#editMod\" onclick='toUpdate("+result[i].id+")' value='修改'>";
						table += "</td>";
						table += "</tr>";

					}
					table += "</tbody>";
					$("#dataList").html(table);
					$("#pageNumber").val(show.pageNumber);
					$("#totalPage").val(show.totalPage);
					$("#pageSize").val(show.pageSize);
					$("#totalCount").val(show.totalCount);

				},
				error: function () {
					alert("系统错误");
				}
			})
		}
		function toPage(flag) {
			var pageNumber = parseInt($("#pageNumber").val());
			var totalPage = parseInt($("#totalPage").val());
			if (flag == 1){
				if (pageNumber <= 1){
					alert("已经是首页了");
					return;
				}
				$("#pageNumber").val(pageNumber-1);
			}
			if (flag == 2){
				if (pageNumber >= totalPage){
					alert("已经是最后一页");
					return;
				}
				$("#pageNumber").val(pageNumber+1);
			}
			if (flag == 3){
				if (pageNumber <= 1){
					alert("已经是首页了");
					return;
				}
				$("#pageNumber").val(1);
			}
			if (flag == 4){
				if (pageNumber >= totalPage){
					alert("已经是尾页了");
					return;
				}
				$("#pageNumber").val(totalPage);
			}
			getData();
		}
		function deleteId(){
			var ids = [];
			$("[name=check]").each(function (i,e) {
				if ($(e).prop("checked")){
					ids.push($(e).val())
				}
			})

			$.ajax({
				url : "/tbTypeTemplateController/deleteAll",
				type : "post",
				data: {ids : ids},
				dataType : "json",
				async : true,
				success : function (result) {
					if (result.code ==10000){
					alert(result.message)
					}
					window.location.reload();

				},
				error : function () {
					alert("网络异常");
				}

			})
		}
	function toUpdate(id) {
			$.ajax({
			url : "/tbTypeTemplateController/toUpdate?id="+id,
			type : "post",
			dataType : "json",
			async : true,
			success : function (result) {
					var list = result.data
					$("#id").val(list.id)
					$("#name1").val(list.name)
					var specIdss= list.specIds
					var brandIdss =list.brandIds
				var customAttributeItemsIdss = list.customAttributeItems
					var specIdsArr= [];
					var brandIdsArr= [];
			 var specIdssJson=	JSON.parse(specIdss);
			 var brandIdssJson=	JSON.parse(brandIdss);
			 var customJson = JSON.parse(customAttributeItemsIdss);
				$.each(specIdssJson,function (i,e) {
					specIdsArr.push(e.id)
				})



				$.each(brandIdssJson,function (i,e) {
					brandIdsArr.push(e.id)
				})
				$("#brand1").val(brandIdsArr).trigger("change");
				$("#spec1").val(specIdsArr).trigger("change");
				var table="";
				for (let i = 0; i < customJson.length; i++) {
					table += "<tr>";
					table += "<td>";
					table += "<input type=\"checkbox\" class=\"icheckbox_square-blue\">";
					table += "</td>";
					table += "<td>";
					table += "<input class=\"form-control\" placeholder=\"属性名称\" id='customAIdsArr1' value="+ customJson[i].text +">";
					table += "</td>";
					table += "<td>";
					table += "<button type=\"button\" class=\"btn btn-default\" title=\"删除\" onclick='deleteTable(this)'><i class=\"fa fa-trash-o\"></i> 删除</button>";
					table += "</td>";
					table += "</tr>";
				}
				$("#updateForm").html(table)

			},
			error : function () {
				alert("网络异常");
			}

		})
	}
	function updateTo() {
		var brands= $("#brand1").select2('data')
		var arrBrands = []
		$.each(brands,function (i,e) {

			var json =	{"id":e.id,"text":e.text}
			arrBrands.push(json)
		})
		var arrJson = JSON.stringify(arrBrands)

		//关联型号
		var spec= $("#spec1").select2('data')
		var arr = []
		$.each(spec,function (i,e) {
			var json =	{"id":e.id,"text":e.text}
			arr.push(json)
		})
		var arrSpecJson = JSON.stringify(arr)

		//商品类型
		var  name = $("#name1").val()

		//获取拓展的值
		var optionList = [];
		var trs = $("#specOption1").find("tr");
		for (let i = 1; i <trs.length; i++) {
			var  tds = $(trs[i]).find("td");
			var orders = $(tds[1]).find("input").val();
			optionList.push({text:orders});
		}
		var optionListJson= JSON.stringify(optionList)

		console.log(name,id,arrJson,arrSpecJson,optionListJson)
		$.ajax({
			url: "/tbTypeTemplateController/update",
			type: "post",
			data:{
				id : $("#id").val(),
				name : name,
				brandIds  : arrJson,
				specIds : arrSpecJson,
				customAttributeItems: optionListJson
			} ,
			dataType: "json",
			async: true,
			success: function (result) {
				if (result.code==10000){
					alert(result.success)
					window.location.reload();

				}else {
					alert(result.message)

				}

			}
		})
	}

	</script>
